function [trl,windowTrigs] = ft_trialfun_VAST_TFR(windowTrigs, settings, reconstruct)
%
% Defines trial epochs using the supplied trigger samples
%   windowTrigs should be a struct with fields "stim1", "dist", "stim2",
%   and "stim2end", as created by VAST_preproc_TFR. Each of these fields is
%   an array of samples at which the indicated trigger occurs, produced by
%   something like windowTrigs.stim1 = eSamps(eVals == 65321);
%
% "reconstruct" (1 or 0) defines whether you're using explicit window
% triggers (41,42,43) or reconstructing start and end samples relative to
% other trigs in the trial.
%
% Outputs: 
%    trl: Nx3 matrix, N = number of trials
%    Col 1) sample of trial start; col 2) sample of trial end; col 3)
%    offset of trigger (0 means sample 1 of each trial is the start trig)
%    - Note that the three trial windows are not separated at this stage.
%
%    windowTrigs: spit back the window trigs with adjusted trigger values.
%    At minimum it will give a simulated trigger for the end of the last
%    stimulus window. If reconstruct==TRUE, also spits back simulated trial
%    window triggers (e.g. 41,42,43).


fs = settings.ds2;
NtimeExtra = 2; % s, length after onset of last STIM2 stimulus to keep in STIM2
                % lengthened to 2s to avoid edge effects
audStimLen = 0.06; % s, duration of the auditory stimulus
audStimSamps = round(audStimLen * fs);
NsamplesExtra = round(NtimeExtra * fs);

if ~ismember(reconstruct,[0 1])
    error('reconstruct must be either 0 or 1.')
end

if ~isfield(windowTrigs,'stim1') || ~isfield(windowTrigs,'dist') ||...
        ~isfield(windowTrigs,'stim2') || ~isfield(windowTrigs,'stim2end')
    error('windowTrigs is not specified properly. See help for this function.')
end

% function-internal shorthand
stim1_st = windowTrigs.stim1;
dist_st = windowTrigs.dist;
stim2_st = windowTrigs.stim2;
stim2_last = windowTrigs.stim2end;


% Test that you have the same number of all samples
if ~isequal(length(stim1_st),length(stim2_last))
    fprintf('\n Start STIM1 trig count: %d', length(stim1_st))
    fprintf('\n End STIM2 trig count: %d \n', length(stim2_last))
    error('Numbers of each trigger not equal (see counts above).')
end


%% Reconstruct the stim window triggers if requested

if reconstruct == 1
    offset_500 = round(0.5*fs);
    offset_560 = round(0.56*fs);
    
    % stim1_st is first STIM1 trigger (10): STIM1 starts 0.5s before this.
    stim1_st = stim1_st - offset_500;
    
    % dist_st is last STIM1 trigger (13): DIST starts 0.5s + 0.06s (stim
    %                                     length) after this.
    dist_st = dist_st + offset_560;

    % stim2_st is first STIM2 trigger (20): STIM2 starts 0.5s before this.
    stim2_st = stim2_st - offset_500;
end

% stim2_last is last STIM2 trigger (23); STIM2 end is [length of aud stim + 
% number of padding samples] after this.
stim2_last_padded = stim2_last + audStimSamps + NsamplesExtra;


%% Define baselines for each trial

baseline_st = stim1_st - fs; % 1 sec baseline
saferead_st = baseline_st - (2*fs); % read 2 sec before baseline to avoid
                                    % edge efx


%% Construct trl

Ntrl = length(stim1_st);
% trl = [baseline_st', stim2_last', zeros(Ntrl,1)]; 
trl = [saferead_st', stim2_last_padded', zeros(Ntrl,1)];


%% refill the modulated stim trigs into windowTrigs

windowTrigs.stim1 = stim1_st;
windowTrigs.dist = dist_st;
windowTrigs.stim2 = stim2_st;
windowTrigs.stim2end = stim2_last;
windowTrigs.baseline_st = baseline_st;



end